/**
 入驻小区
 **/
(function(vc) {
    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
        },
        data: {
            chainSupplierTreeInfo: {
                suppliers: [],
                csId: ''
            }
        },
        _initMethod: function() {
            $that._loadChainSuppliers();

        },
        _initEvent: function() {
            vc.on('chainSupplierTree', 'refreshTree', function(_param) {
                if (_param) {
                    $that.chainSupplierTreeInfo.csId = _param.csId;
                }
                $that._loadChainSuppliers();
            });
        },
        methods: {

            _loadChainSuppliers: function() {

                let param = {
                    params: {
                        csId: '',
                        page: 1,
                        row: 50
                    }
                };
                //发送get请求
                vc.http.apiGet('/chainSupplier.listChainSupplier',
                    param,
                    function(json) {
                        let _unitInfo = JSON.parse(json);
                        $that.chainSupplierTreeInfo.suppliers = _unitInfo.data;
                        $that._initJsTreechainSupplier();
                    },
                    function() {
                        console.log('请求失败处理');
                    });
            },
            _initJsTreechainSupplier: function() {

                let _data = $that._doJsTreeData();

                let _catalogId = '';

                $that.chainSupplierTreeInfo.suppliers.forEach(item => {
                    if ($that.chainSupplierTreeInfo.csId && item.csId == $that.chainSupplierTreeInfo.csId) {
                        _catalogId = item.catalogId;
                    }
                })

                $.jstree.destroy()
                $("#jstree_chainSupplier").jstree({
                    "checkbox": {
                        "keep_selected_style": false
                    },
                    'state': { //一些初始化状态
                        "opened": true,
                    },
                    // 'plugins': ['contextmenu'],
                    'core': {
                        'data': _data
                    },
                });
                $("#jstree_chainSupplier").on("ready.jstree", function(e, data) {
                    //data.instance.open_all();//打开所有节点
                    if (_catalogId) {
                        $('#jstree_chainSupplier').jstree('select_node', 'c_' + _catalogId /* , true */ );
                        return;
                    }
                    $('#jstree_chainSupplier').jstree('select_node', _data[0].children[0].id /* , true */ );

                });

                $('#jstree_chainSupplier').on("changed.jstree", function(e, data) {
                    if (data.action == 'model' || data.action == 'ready') {
                        //默认合并
                        //$("#jstree_chainSupplier").jstree("close_all");
                        return;
                    }
                    let _selected = data.selected[0];

                    if (_selected.startsWith('cs_')) {
                        vc.emit($props.callBackListener, 'switchSupplier', {
                            csId: data.node.original.csId
                        })
                        return;
                    }

                    //console.log(_selected, data.node.original.catalogId)
                    vc.emit($props.callBackListener, 'switchCatalog', {
                        csId: data.node.original.csId,
                        catalogId: data.node.original.catalogId
                    })
                });


            },
            _doJsTreeData: function() {
                let _mSupplierTree = [];

                let _suppliers = $that.chainSupplierTreeInfo.suppliers;

                //构建 第一层菜单组
                _suppliers.forEach(pItem => {
                    let _includeSupplier = false;
                    for (let _mgIndex = 0; _mgIndex < _mSupplierTree.length; _mgIndex++) {
                        if (pItem.csId == _mSupplierTree[_mgIndex].csId) {
                            _includeSupplier = true;
                        }
                    }
                    if (!_includeSupplier) {
                        let _supplierItem = {
                            id: 'cs_' + pItem.csId,
                            csId: pItem.csId,
                            name: pItem.name,
                            icon: "/img/supplier.png",
                            text: pItem.name,
                            state: {
                                opened: false
                            },
                            children: []
                        };
                        $that._doJsTreeMenuData(_supplierItem, pItem);
                        _mSupplierTree.push(_supplierItem);
                    }
                });
                return _mSupplierTree;
            },
            _doJsTreeMenuData: function(_supplierItem, _pItem) {
                let _suppliers = $that.chainSupplierTreeInfo.suppliers;
                //构建菜单
                let _children = _supplierItem.children;

                _pItem.catalogs.forEach(_item => {
                    let _menuItem = {
                        id: 'c_' + _item.catalogId,
                        catalogId: _item.catalogId,
                        text: _item.catalogName,
                        catalogName: _item.catalogName ,
                        csId: _item.csId ,
                        icon: "/img/supplierType.png"
                    };
                    _children.push(_menuItem);
                })
            },

        }
    });
})(window.vc);